package com.laipai.cameraCheck.app;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.fileupload.FileItem;
import org.springframework.stereotype.Service;

import com.laipai.app.annotation.NotLogin;
import com.laipai.app.common.JSONTools;
import com.laipai.app.common.Tools;
import com.laipai.app.process.app.RequestExecutor;
import com.laipai.app.process.app.dataobj.BaseRequestParameters;
import com.laipai.base.util.LaipaiConstants;
import com.laipai.cameraCheck.pojo.LpCameramanAppform;
import com.laipai.cameraCheck.pojo.LpInvite;
import com.laipai.cameraCheck.service.ICameraCheckservice;
import com.laipai.cameraCheck.service.IInviteService;
import com.laipai.userManInfo.dto.LpUserBean;
import com.laipai.userManInfo.pojo.LpUser;
import com.laipai.userManInfo.service.IUserManInfoService;

@NotLogin
@Service("CameramanAppformPicAndroidImpl")
public class CameramanAppformPicAndroidImpl implements RequestExecutor {
	@Resource(name=IUserManInfoService.SERVICE_NAME)
	private IUserManInfoService userManInfoService;
	@Resource(name=ICameraCheckservice.ICAMERACHECKSERVICE_NAME)
	private ICameraCheckservice cameraCheckservice;
	@Resource(name=IInviteService.IINVITESERVICE_NAME)
    private IInviteService inviteService;
	@Override
	public Object execute(BaseRequestParameters parameters,
			Object... requestObjects) {
JSONObject json = parameters.getJson();
		
		//文件保存目录
	    String filePath = LaipaiConstants.UPLOAD_ABSOLUTE_IMG+LpUserBean.LP_USER_IMGURL;    
	  //文件列表
	  Set<Map.Entry<String, FileItem>> set = parameters.getFileMap().entrySet();
	  //登录人id
	  String userId = json.getString("userId");	
	  String invitecode=JSONTools.getString(json,"invite");
	  LpUser user= userManInfoService.queryUserById(userId);
	    
	    if(userId==null||invitecode==null){
	    	
	    	return JSONTools.newAPIResult(1, "参数不全！");
	    	
	    }
	  
	   StringBuffer sb=new StringBuffer();
		if(set!=null && set.size() > 0){
			File files = new File(filePath);
			if (!files.exists() || !files.isFile()) {
				System.out.println("--------用户保存路径成功:"+filePath);
				files.mkdir();
			}
			try {
				for (Map.Entry<String, FileItem> f : set) {
					String filename = f.getKey();
					if(filename.contains("idCard")){
						System.out.println("-----------保存文件:"+filename);
						int index = filename.lastIndexOf(".");
						System.out.println("-----------位置:"+filename);
			            if(index != -1){
			               filename = filename.substring(index);
			            }
						InputStream in= f.getValue().getInputStream();
						String realFileName =UUID.randomUUID()+filename;
					/*	sb.append(realFileName+";");*/
						Tools.writeFile(filePath +"/"+ realFileName, in);
						String IdImg= this.getRelativePath(realFileName);
						user.setIdCardImage(IdImg);
						userManInfoService.saveOrUpdateser(user);	
						
					}else{
						System.out.println("-----------保存文件:"+filename);
						int index = filename.lastIndexOf(".");
						System.out.println("-----------位置:"+filename);
			            if(index != -1){
			               filename = filename.substring(index);
			            }
						InputStream in= f.getValue().getInputStream();
						String realFileName =UUID.randomUUID()+filename;
						sb.append(realFileName+";");
						Tools.writeFile(filePath +"/"+ realFileName, in);
						
					}
					
				}
				LpCameramanAppform appform =new LpCameramanAppform();
				appform.setApplyTime(new Timestamp(new java.util.Date().getTime()) );
				appform.setCheckStatus(0);
				appform.setLpUser(user);
				/**上传完之后的图片名称*/
				String [] imageNameArray=sb.toString().split(";");
				for(int i=0;i<imageNameArray.length;i++){
					
					if(i==0){
						appform.setPhotoOne(this.getRelativePath(imageNameArray[0]));
					}else if(i==1){
						appform.setPhotoTwo(this.getRelativePath(imageNameArray[1]));
					}else if(i==2){
						appform.setPhotoThree(this.getRelativePath(imageNameArray[2]));
					}
				}
				cameraCheckservice.saveAppform(appform);
				
				
			}catch (IOException e1) {
				e1.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		 JSONObject resulejson = new JSONObject();
		 
		 LpInvite invite=inviteService.getInvite(invitecode);
		 if(invite!=null){
		 invite.setLpUser(user);
		 invite.setInviteStatus(1);
		 inviteService.updateInvite(invite);
		 }
	     resulejson.put("resultMessage", "申请成功");
	     return resulejson;
	}
	private String getRelativePath(String fileName){
		String savePath=LaipaiConstants.UPLOAD_ABSOLUTE_IMG + LpUserBean.LP_USER_IMGURL+"/"+fileName;		
		return savePath;
	}
}
